// pages/me/editSelf/editSelf.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        modalName: null,
        userInfo: {},
        userEdit:{
            userName:'', 
            userNameStatus: false,
            mobile:'',
            verifyCode:'',
            wechat:'',
            wechatStatus: false,
        },
        verifyCodeButtonDisabled: false,
        codeMinute: 60, //验证码获取倒计时60S
    },

    /**
     * 生命周期函数--监听页面加载
     */
    doNothing(){},
onLoad: function (options) {
        this.InitUserStatus();
    },
    onReady: function(){
        this.chooseLogo = this.selectComponent('#chooseLogo');
    },
    // 检查一下登录状态。
    async InitUserStatus(){
        app.showLoading("获取用户信息");
        await app.checkToken();
        this.InitUserInfo();
        wx.hideLoading();
    },
    InitUserInfo(){
        this.setData({
            'userInfo': app.globalData.userInfo
        })
    },
    // 上传用户头像
    userModifylogo(){
        let _this = this;
        this.chooseLogo.uploadFiles('dealerUserLogo',0).then(res =>{
            if(res.length>0){
                app.sHttp.userModifylogo(res[0].originalUrl).then(res =>{
                    app.success('更换成功');
                    // 隐藏弹出框，刷新用户信息。
                    _this.hideModal();
                    app.reloadUserInfo();
                    _this.InitUserStatus();
                },err=>{
                   app.error(err.msg||err);
                })
            }else{
                app.error("获取头像失败");
            }
        },err=>{
           app.error(err.msg||err);
        })
    },
    // 修改昵称或者微信号。
    doEditUser(e){
        let changeItem = e.currentTarget.dataset.target;
        let userEdit = this.data.userEdit;
        let InputValue = userEdit[changeItem];
        if ('' != InputValue) {
            // 执行修改内容
            let methodName = "";
            switch (changeItem) {
                case 'userName':
                    methodName = "userModifyName";
                    break;
                case 'wechat':
                    methodName = "userModifyWechat";
                    break;
                default:
                    app.error('请退出重试');
                    return;
            }
            app.sHttp[methodName](InputValue).then(res =>{
                app.success('修改成功');
                // 重置编辑模式.刷新用户信息。
                this.resetUserEdit();
                app.reloadUserInfo();
                this.InitUserStatus();
            },err=>{
               app.error(err.msg||err);
            })
        }else{
            app.alert('不能修改为空白哦');
        }
    },
    editUser(e){
        let changeItem = e.currentTarget.dataset.target;
        let userEdit = this.data.userEdit;
        userEdit[changeItem] = !userEdit[changeItem];
        this.setData({
            'userEdit':userEdit
        })
    },
    // 用户注册。
    userChangeMoblie(){
        // 校验手机号，验证码是否已经填写。
        if(this.validatemobile(this.data.userEdit.mobile)){
        // 手机号正确。验证验证码长度。
        if(this.data.userEdit.verifyCode.length != 6){
            app.alert("验证码为6位数字");
            return
        }else{
            let yuanshouji = this.data.userInfo.userMobile.replace(/\s/g,"");
            app.sHttp.userModifyMobile({'oldMobile':yuanshouji,'newMobile':this.data.userEdit.mobile,'newVerifyCode':this.data.userEdit.verifyCode}).then(res =>{
                app.success("修改成功");
                // 隐藏弹出框，刷新用户信息。
                this.hideModal();
                this.resetUserEdit();
                app.reloadUserInfo();
                this.InitUserStatus();
            },err=>{
                app.error(err.msg);
            })
        }
        }
    },
    // 获取手机验证码。
    getVerifyCode(){
      if(this.validatemobile(this.data.userEdit.mobile)){
        let yuanshouji = this.data.userInfo.userMobile;
        if (yuanshouji.replace(/\s/g,"") == this.data.userEdit.mobile) {
            app.alert('请输入新手机号');
            return false;
        }
        this.setData({
          verifyCodeButtonDisabled: true
        });
        // 执行获取验证码流程
        app.sHttp.smsSend({mobile: this.data.userEdit.mobile,smsType:2,isNewMobile:1}).then(res =>{
          app.alert('验证码已发送至您的手机');
          this.codeCountDown();
        },err=>{
          app.error(err.msg);
          this.setData({
            verifyCodeButtonDisabled: false
          });
        })
      }
    },
    // 验证码倒计时工具。
    codeCountDown(){
      var that = this;
      this.minuteInterval = setInterval(() => {
        var codeMinute = that.data.codeMinute;
        codeMinute--;
        that.setData({
          codeMinute: codeMinute
        });
        if(codeMinute == 0){
          clearInterval(that.minuteInterval);
          that.setData({
            codeMinute: 60,
            verifyCodeButtonDisabled: false
          })
        }
      }, 1000);
    },
    
    bindRegisterParam(e){
        let userEdit = this.data.userEdit;
        userEdit[e.currentTarget.dataset.name] = e.detail.value;
        this.setData({
            'userEdit' : userEdit
        })
    },
    userMsgInput(e){
        let changeItem = e.currentTarget.dataset.target;
        let userEdit = this.data.userEdit;
        userEdit[changeItem] = e.detail.value;
        this.setData({
            'userEdit':userEdit
        })
    },
    showModal(e) {
        console.log(e);
        this.setData({
            modalName: e.currentTarget.dataset.target
        })
    },
    hideModal(){
        this.setData({
            modalName: null
        })
    },
    resetUserEdit(){
        this.setData({
            'userEdit': {
                userName:'', 
                userNameStatus: false,
                phoneNumber:'',
                verifyCode:'',
                wechat:'',
                wechatStatus: false,
            }
        })
    },
    validatemobile(mobile) {
      if (mobile.length == 0) {
        app.alert('请输入手机号');
        return false;
      }
      if (mobile.length != 11) {
        app.alert('请输入正确的手机号');
        return false;
      }
      var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
      if (!myreg.test(mobile)) {
        app.alert('请输入正确的手机号');
        return false;
      }
      return true;
     }
})